<?php
/**
 * The model file of blog module of ZenTaoPHP.
 *
 * The author disclaims copyright to this source code.  In place of
 * a legal notice, here is a blessing:
 * 
 *  May you do good and not evil.
 *  May you find forgiveness for yourself and forgive others.
 *  May you share freely, never taking more than you give.
 */
?>
<?php
class sysmanageModel extends model
{
	 
   /**
     * Get 用户信息列表
     * 
     * @access public
     * @return array
     */
    public function get_user_info($pager = null)
    {
    	
        return $this->dao->select('*')->from('user_info')->orderBy('userid asc')->page($pager)->fetchAll();
    }
    
     /**
     * 根据用户名得到用户信息
     * @param unknown_type $username
     */
    public function get_user_info_by_username($username){
    	$data=$this->dao->select('*')->from('user_info')->where("username='$username'")->fetch();
        return $data;
    }
    
    public function is_user_exist($username,$pwd){
    	$pwd=md5($pwd);
        $data=$this->dao->select('*')->from('user_info')->where("username='$username' and pwd='$pwd'")->fetch();
        return $data;
    }  
    
    /**
     * 创建用户信息表
     */
    public function create_user_info(){
    	$user_info = fixer::input('post')->specialchars('username,pwd,status')->get();
    	$data=new stdClass();
    	$data->username=$user_info->username;
    	$data->pwd=md5($user_info->pwd);
    	$data->status=$user_info->status;
    	if($this->get_user_info_by_username($data->username)){//假如帐号已经存在，则不添加
    	   return false;
    	}
        $this->dao->insert('user_info')->data($data)->autoCheck()->batchCheck('username,pwd','notempty')->exec();
        return $this->dao->lastInsertID();
    }
    /**
     * 更新用户信息表
     */
    public function update_user_info($data,$userid){
        $this->dao->update('user_info')->data($data)->where('userid')->eq($userid)->exec();
    }
    
   /**
     * 删除用户信息
     * @param $id
     */
    public function delete_user_info($userid)
    {
        $this->dao->delete()->from('user_info')->where('userid')->eq($userid)->exec();
    }
   
    /**
     * Get 得到操作日志
     * 
     * @access public
     * @return array
     */
    public function get_optlog($pager = null,$where_to=null)
    {
        $where="1=1";
        if(trim($where_to->key_word)!=''){
    		$where.=" and (details like '%".$where_to->key_word."%' or username like '%".$where_to->key_word."%')";
    	}
    	//时间判断
        if(trim($where_to->datetime_start)!=''){
    		$where.=" and opt_time >".strtotime(trim($where_to->datetime_start));
    	}
        if(trim($where_to->datetime_end)!=''){
    		$where.=" and opt_time <".strtotime(trim($where_to->datetime_end));
    	}
        return $this->dao->select('*')->from('optlog')->where($where)->orderBy('optlog_id desc')->page($pager)->fetchAll();
    }
    /**
     * 添加系统日志
     */
    public function set_optlog($details){
    	$data=new stdClass();
    	$data->username=$_SESSION['username'];
    	$data->opt_time=time();
    	$data->details=$details;
        $this->dao->insert('optlog')->data($data)->exec();
        return $this->dao->lastInsertID();
    }
    /**
     * 得到服务器的状态
     */
    public function get_serverstatus(){
        $uptime = shell_exec('uptime');
		$tmp = explode("average:",$uptime);
		$inf["loads"]=trim($tmp[1]);//CPU 负载 (1 / 5 / 15 分钟)
		$uptime2 = @file('/proc/uptime');
		$uptime2 = implode("", $uptime2);
		$tmp = explode(".",$uptime2);
		$inf["uptime"]=trim($tmp[0]);//开机时间
		$uptime3 = shell_exec('date +%s');
		$inf["time"]=date('Y-m-d H:i:s',trim($uptime3));//系统时间
		$hostname = shell_exec('uname -n');
		$inf["hostname"] = trim($hostname);//系统名称
		
		$meminfo = @file("/proc/meminfo");
		$meminfo = implode("", $meminfo);
		preg_match_all("/MemTotal\s{0,}\:+\s{0,}([\d\.]+).+?MemFree\s{0,}\:+\s{0,}([\d\.]+).+?SwapTotal\s{0,}\:+\s{0,}([\d\.]+).+?SwapFree\s{0,}\:+\s{0,}([\d\.]+)/s", $meminfo, $buf);
		
		$inf['memTotal'] = round($buf[1][0]*1024, 2);//总内存
		$inf['memFree'] = round($buf[2][0]*1024, 2);//剩余内存
		$inf['memUsed'] = ($inf['memTotal']-$inf['memFree']);//已使用
		$inf['memPercent'] = (floatval($inf['memTotal'])!=0)?round($inf['memUsed']/$inf['memTotal']*100,2):0;
		//百分比
		
		//SWAP
		$inf['swapTotal'] = round($buf[3][0]*1024, 2);
		$inf['swapFree'] = round($buf[4][0]*1024, 2);
		$inf['swapUsed'] = ($inf['swapTotal']-$inf['swapFree']);
		$inf['swapPercent'] = (floatval($inf['swapTotal'])!=0)?round($inf['swapUsed']/$inf['swapTotal']*100,2):0;
		return $inf;
    }
    /*********************************************************************************************/
    
}
